﻿using DG.Tweening;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 仿海岛奇兵的弹窗动画
/// </summary>
public class WindowAnimation01
{
    private struct GraphicState
    {
        public Graphic graphic;
        public float originalAlpha;
    }
    // 添加缓存优化和动画参数配置
    public float openScaleDuration = 0.3f;
    public float closeScaleDuration = 0.3f;
    public float fadeDuration = 0.3f;
    public Vector3 openStartScale = Vector3.one * 1.2f;
    public Vector3 closeEndScale = Vector3.one * 0.8f;
    public Ease scaleOpenEase = Ease.OutBack;
    public Ease scaleCloseEase = Ease.InBack;
    public Ease fadeEase = Ease.OutQuad;
    private Transform popWindow;

    private Sequence currenSequence;

    private List<GraphicState> _windowGraphics = new List<GraphicState>(); 

    public void Init(Transform popWindow, Transform mask=null)
    {
        this.popWindow = popWindow;
        Graphic graphic = mask?.GetComponent<Graphic>();
        CacheWindowComponents(graphic);
    }
    public void OpenWindowAnimation(Action onComplete = null,float delay =0)
    {
        currenSequence?.Kill();
     
        currenSequence = PlayWindowAnimation(true)
            .SetDelay(delay)
            .OnStart(() =>
            {
                ResetWindowElementsState(true);
                popWindow.gameObject.SetActive(true);
                popWindow.transform.localScale = openStartScale;
            }
            )
           .OnComplete(() =>
           {
               onComplete?.Invoke();
           });
    }
    public void CloseWindowAnimation(Action onComplete = null, float delay = 0)
    {
        currenSequence?.Kill();
        currenSequence = PlayWindowAnimation(false)
            .SetDelay(delay)
             .OnStart(() =>
             {
                 ResetWindowElementsState(false);
             }
            )
            .OnComplete(() =>
            {
                onComplete?.Invoke();
            });

    }

   
    private void ResetWindowElementsState(bool forOpening)
    {
        foreach (var graphic in _windowGraphics)
        {
            Color c = graphic.graphic.color;
            graphic.graphic.color = new Color(c.r, c.g, c.b, forOpening ? 0 : c.a);
        }
    }
    private void CacheWindowComponents(Graphic mask =null)
    {
        _windowGraphics.Clear();

        if (mask != null)
        {
            _windowGraphics.Add(new GraphicState
            {
                graphic = mask,
                originalAlpha = mask.color.a
            }) ;
        }
        foreach (var graphic in popWindow.GetComponentsInChildren<Graphic>(true))
        {
            _windowGraphics.Add(new GraphicState
            {
                graphic = graphic,
                originalAlpha = graphic.color.a 
            });
        }
    }
    private Sequence PlayWindowAnimation(bool isOpening)
    {
        Sequence sequence = DOTween.Sequence();

        float scaleDuration = isOpening ? openScaleDuration : closeScaleDuration;
        Ease scaleEase = isOpening ? scaleOpenEase : scaleCloseEase;
        Vector3 targetScale = isOpening ? Vector3.one : closeEndScale;
        // 缩放动画
        popWindow.DOScale(targetScale, scaleDuration)
                 .SetLink(popWindow.gameObject)
          
             .SetEase(scaleEase);
        // 透明度动画
        foreach (var graphic in _windowGraphics)
        {
            float targetAlpha = isOpening ? graphic.originalAlpha : 0;
            //Color originalColor = graphic.graphic.color;

            //if (isOpening)
            //{
            //    graphic.graphic.color = new Color(originalColor.r, originalColor.g, originalColor.b, 0);
            //}

            sequence.Join(graphic.graphic.DOFade(targetAlpha, fadeDuration).SetEase(fadeEase));

        }

        return sequence;
    }
}